<?php
# header of each page
function head($title='', $meta_description='', $meta_keywords='', $active='') {
header("Content-type: text/html; charset=UTF-8");
?>
<!DOCTYPE html>
<html>
<head>
<title><?=$title?></title>
<meta name="description" content="<?=$meta_description?>" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">        
<link rel="stylesheet" href="<?=HTML_BASE?>css/normalize.min.css">
<link rel="stylesheet" href="<?=HTML_BASE?>css/prettyPhoto.css">
<link rel="stylesheet" href="<?=HTML_BASE?>css/main.css">
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300&subset=latin,cyrillic' rel='stylesheet' type='text/css'>
</head>
<body>
   <nav id="top-nav-menu">
        <div id="lang">
        <?php 
				$q_string = '';
				//if($_REQUEST['prm1']) $q_string .= '/'.$_REQUEST['prm1'];
				//if($_REQUEST['prm2']) $q_string .= '/'.$_REQUEST['prm2'];
				
				$sql_lang = 'SELECT code FROM language WHERE published ORDER BY order_number '; 
				$lres = query($sql_lang);
				while ($lang = mysql_fetch_object($lres)) {
					$active = '';
					if($_SESSION['lang'] == $lang->code) $active = 'class="active"';
		?>
         			 <a href="<?=HTML_BASE.$lang->code.$q_string?>"><div id="<?=$lang->code?>" <?=$active?> ><?=$lang->code?></div></a>
           
        <?php   }?>
        </div>
        <div id="search-field">
           <input type="text" name="search" id="search" value="<?=$_SESSION['search']?>" placeholder="<?=__Search?> ..." required>
           <button id="search-button" onClick="window.location.href = '<?=HTML_BASE.$_SESSION['lang']?>/search/'+ $('#search').val();"></button>
        </div>
          
            <?php
			   
			 
			 $sql= "SELECT menuitems FROM menus WHERE menu_id = '2'";
			 $result = query($sql);
			 $row_m = mysql_fetch_object($result);
			 
			 $arr =  json_c_decode($row_m->menuitems);
			 
			 function create_ul($ar,$first)
			 {
					$menu_id = 2;
					echo '<ul>';
					
					
					foreach ($ar as $key => $value) {
					 
					
						$item = $value['id'];
						
						$sqlm = "SELECT parent_id,title FROM  menu_description 
							  WHERE menu_id = '".$menu_id."'AND item_id = '".sth($item)."'
							  AND lang_id = '".$_SESSION['lang_id']."'
							 ";
					
						$rowm = mysql_fetch_object(query($sqlm)); 
					
						$sql_sef = "SELECT sef_url FROM  sef_url 
							  WHERE query = '".$rowm->parent_id."' AND tamplate='page' AND lang_id = '".$_SESSION['lang_id']."'
							 ";
						$row_sef = mysql_fetch_object(query($sql_sef));
						
						$active = '';
						if($row_sef->sef_url == $_GET['prm1']) $active = 'class="active"';
						
						if($row_sef->sef_url == 'home' && !$_GET['prm1']) $active = 'class="active"';
						$parent = '';
						if($value['children'])$parent = ' class="isparent" ';
						
						$href = 'href="#"';
						if(!$value['children']) $href = 'href="'.HTML_BASE.$_SESSION['lang'].'/'.$row_sef->sef_url.'"';
						
						
						
						
						echo '<li'.$parent.'><a '.$active.' '.$href.' >'.$rowm->title.'</a>';
						 
						//if($value['children']) create_ul($value['children'],false);
						 
						echo '</li>';
					}
						
						
						echo '</ul>';
					
					
			 }
			 
			 create_ul($arr,true);
					
		?>
        <div id="close-button">X</div>
        <div id="copyright">
           <p>&copy Onix <?=date('Y')?></p>
           <p>Website by <a href="http://studiokipo.com" target="_blank">Kipo</a></p>
        </div>          
    </nav> <!--# top nav menu-->
   
        
<?php	
}

# footer of each page
function foot($tmp='') {
	
?>
		
	 </div> <!--# wrapper--> 
    
    <!--vendor scripts-->
    <script src="<?=HTML_BASE?>lib/jquery-1.11.1.min.js"></script>
    <script src="<?=HTML_BASE?>lib/jquery.shuffle.modernizr.js"></script> 
    <script src="<?=HTML_BASE?>lib/jquery.prettyPhoto.js"></script>	  
    <!--app scripts-->
    <script src="<?=HTML_BASE?>js/main.js"></script>
</body>
<?php
}

# Get category filter
function getCategory() {
	
?>
	<div id="filter" class="cf">
                    <ul class="cf">
                        <li><a href="#" class="active" data-group="all"><?=__All?></a></li>
                        <?php
                        		$sql = "SELECT c.class, d.Name FROM
										category AS c,
										category_description AS d
									WHERE
			  							c.category_id = d.category_id
									AND
									    d.lang_id = '".$_SESSION['lang_id']."' 
										AND c.published
										ORDER BY c.order_number DESC";
								$result = query($sql);
						while ($row = mysql_fetch_object($result)) {
						?>
                        
                        <li><a href="#" data-group="<?=$row->class?>"><?=$row->Name?></a></li>
                     
                        <?php }?>
                    </ul>
    </div>
<?php
}

# Get category filter
function getClass($project_id) {

       $sql = "SELECT c.class FROM
										category AS c,
										project_to_category AS p2c
									WHERE
			  							c.category_id = p2c.category_id
										AND
										p2c.project_id = '".$project_id."'
										AND c.published
										ORDER BY c.order_number DESC";
						$result = query($sql);
						$class = '';
						while ($row = mysql_fetch_object($result)) {
							$class .= '"'.$row->class.'",';
						}
						$class = substr($class,0,-1);
		return $class;
}

# a redirecting function that adds the session ID if cookies are disabled and stops the execution of the script
function redirect($location) {
	if (!$location) error("Missing arguments.");

	$a = parse_url($location);
	if (!isset($a['query'])) {
		$a['query'] = '';
	}
	
	$session_delimiter = ($a['query'])?'&':'?';
	header("Location: $location".addsession($session_delimiter));
	exit;
}

# adding the session_id when cookies are disabled
function addsession($symbol="?") {
	return (!isset($_COOKIE[session_name()])&&session_id())?$symbol.session_name()."=".session_id():"";
}

function escape($string,$encoding=false) {
  return mysql_real_escape_string( (($encoding)?encoding($string):$string) );
}

function encoding($string) {
  return iconv('ISO-8859-1', 'UTF-8', $string);
}

function sth($string) {
  return htmlspecialchars(stripslashes($string));
}

# checks whether an email is valid or not
function checkEmail($email) {
	return preg_match("/^[A-z0-9\.\-]+@(?:[A-z0-9\-]+\.)+[A-z0-9]{1,6}$/", $email);
}

# Cut a text to specified chracters length
function text_format($text, $length) {
	$str = '';
	$text_length = mb_strlen($text, "UTF-8");
	 
	if ($text_length < $length) {
		$str = $text;		
	} else {
		$position_end = strpos($text, ' ', $length);
		$str = substr($text, 0, ($position_end)?$position_end:$text_length);
	}
	
	return $str;
}

function text_format_blog($text, $length) {
	$str = '';
	$text_length = mb_strlen($text, "UTF-8");
	 
	if ($text_length < $length) {
		$str = $text;		
	} else {
		$position_end = strpos($text, '<hr />', $length);
		$str = substr($text, 0, ($position_end)?$position_end:$text_length);
	}
	
	return $str;
}


# general error function
function error($s="An error occured.") {
  if (!headers_sent()) {
    head("Error");
    echo "<table cellpadding=4 width=".SITE_WIDTH." align=center border=0><tr><td align=center class=title>$s</td></tr></table>";
    foot();
  }
  else
    echo $s;
  exit;
}

# debug function for dumping a variable value
function pp($var, $stop=false) {
  echo "<pre>";
  print_r($var);
  echo "</pre>";
  if ($stop) exit;
}

# Generate numbers
function generateNumber () {

  # start with a blank number
  $number = "";

  # define possible characters
  $possible = "0123456789"; 

  # pick a random character from the possible ones
  $number = substr($possible, mt_rand(0, strlen($possible)-1), 1);

  # done!
  return $number;

}



function json_c_decode($json, $assoc = true, $depth = 512, $options = 0) {
			
				$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);
				
				if(version_compare(phpversion(), '5.4.0', '>=')) {
					$json = json_decode($json, $assoc, $depth, $options);
				}
				elseif(version_compare(phpversion(), '5.3.0', '>=')) {
					$json = json_decode($json, $assoc, $depth);
				}
				else {
					$json = json_decode($json, $assoc);
				}
			
				return $json;
		}



?>